package com.sansi.pencilbook.service.impl;

import com.sansi.pencilbook.DO.Result;
import com.sansi.pencilbook.Enum.BehaviorEnum;
import com.sansi.pencilbook.entity.UserBook;
import com.sansi.pencilbook.mapper.UserBookMapper;
import com.sansi.pencilbook.service.UserBookServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * 功能说明: 用户行为的相关业务
 *
 * @author lsy13
 */
@Service
public class UserBookServerImpl implements UserBookServer {

    @Autowired
    private UserBookMapper userBookMapper;

    @Override
    public Result getAllUserBook() {
        try {
            List<UserBook> list = userBookMapper.findAll();
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getUserBookByUser(int userId) {
        try {
            List<UserBook> list = userBookMapper.findByUser(userId);
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getUserBookByBook(String isbn) {
        try {
            List<UserBook> list = userBookMapper.findByBook(isbn);
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getUserBookByBehavior(BehaviorEnum behaviorEnum) {
        try {
            List<UserBook> list = userBookMapper.findByBehavior(behaviorEnum.getBehavior());
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getUserBookByUserBehavior(int userId, BehaviorEnum behaviorEnum) {
        try {
            List<UserBook> list = userBookMapper.findByUserBehavior(userId, behaviorEnum.getBehavior());
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getUserBookByUserBook(int userId, String isbn) {
        try {
            List<UserBook> list = userBookMapper.findByUserBook(userId, isbn);
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result getUserBookByUserBookBehavior(int userId, String isbn, BehaviorEnum behaviorEnum) {
        try {
            List<UserBook> list = userBookMapper.findByUserBookBehavior(userId, isbn, behaviorEnum.getBehavior());
            return list.size()>0 ? Result.ofSuccess("查询成功", list.size(), list) :
                Result.ofFailed("查询失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result addUserBook(UserBook userBook) {
        try {
            int num = userBookMapper.saveUserBook(userBook);
            return num==1 ? Result.ofSuccess("插入成功", 1) :
                Result.ofFailed("插入失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }

    @Override
    public Result addUserBook(BehaviorEnum behaviorEnum, String isbn, int userId) {
        Calendar calendar = Calendar.getInstance();
        UserBook userBook = new UserBook();
        userBook.setIsbn(isbn);
        userBook.setUserId(userId);
        userBook.setBehavior(behaviorEnum.getBehavior());
        userBook.setTime(new SimpleDateFormat("yyyyMMddHHmm").format(calendar.getTime()));
        try {
            int num = userBookMapper.saveUserBook(userBook);
            return num==1 ? Result.ofSuccess("插入成功", 1) :
                Result.ofFailed("插入失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }


    @Override
    public Result deleteUserBook(UserBook userBook) {
        try {
            int num = userBookMapper.deleteUserBook(userBook);
            return num==1 ? Result.ofSuccess("删除成功", 1) :
                Result.ofFailed("删除失败");
        } catch (SQLException throwables) {
            System.out.println(throwables.getMessage());
            return Result.ofFailed(throwables.getMessage());
        }
    }
}
